package com.uy.cuantoconocesdeuruguay.controllers;

import java.io.PrintWriter;
import java.io.StringWriter;

import android.app.Activity;

import com.google.android.gms.analytics.HitBuilders;
import com.google.android.gms.analytics.StandardExceptionParser;
import com.google.android.gms.analytics.Tracker;
import com.uy.cuantoconocesdeuruguay.AnalyticsSampleApp;
import com.uy.cuantoconocesdeuruguay.AnalyticsSampleApp.TrackerName;
import com.uy.cuantoconocesdeuruguay.utiles.StringPool;

public class GoogleAnalyticsTracker {

	public static void trackException(Activity activity, Exception e) {
		if (e == null){
			e = new Exception("Stub exception");
		}
		StringWriter errors = new StringWriter();
		e.printStackTrace(new PrintWriter(errors));
		String stackTrace = errors.toString();
		Tracker t = ((AnalyticsSampleApp) activity.getApplication())
				.getTracker(TrackerName.APP_TRACKER);

		t.send(new HitBuilders.ExceptionBuilder()
				.setDescription(
					new StandardExceptionParser(activity, null).getDescription(
							stackTrace+StringPool.LINE_FEED+Thread.currentThread().getName(), e))
				.setFatal(false).build());
	}

	public static void trackScreen(Activity activity, String title) {

        Tracker t = ((AnalyticsSampleApp)activity.getApplication()).getTracker(TrackerName.GLOBAL_TRACKER);
        t.setScreenName(title);
        t.send(new HitBuilders.AppViewBuilder().build());
	}

}
